using System;
using System.Windows.Forms;
using LicenceManager.Core;

namespace LicenceManager.Client
{
    public partial class Form1 : Form
    {
        private readonly ILicenceService _licenceService;

        public Form1()
        {
            InitializeComponent();
            _licenceService = new LicenceService();
            InitializeUI();
        }

        private void InitializeUI()
        {
            // 显示硬件ID
            txtHardwareId.Text = _licenceService.GetHardwareId();

            // 设置Licence文件默认路径
            txtLicencePath.Text = AppDomain.CurrentDomain.BaseDirectory + "licence.dat";

        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                if (!int.TryParse(txtDuration.Text, out var duration) || duration <= 0)
                {
                    MessageBox.Show("请输入有效的时长(正整数)");
                    return;
                }

                if (cboDurationUnit.SelectedItem == null)
                {
                    MessageBox.Show("请选择时长单位");
                    return;
                }

                var unit = cboDurationUnit.SelectedItem.ToString();
                var expiryDate = DateTime.Now;

                switch (unit)
                {
                    case "秒":
                        expiryDate = expiryDate.AddSeconds(duration);
                        break;
                    case "分钟":
                        expiryDate = expiryDate.AddMinutes(duration);
                        break;
                    case "小时":
                        expiryDate = expiryDate.AddHours(duration);
                        break;
                    case "天":
                        expiryDate = expiryDate.AddDays(duration);
                        break;
                    case "月":
                        expiryDate = expiryDate.AddMonths(duration);
                        break;
                    case "年":
                        expiryDate = expiryDate.AddYears(duration);
                        break;
                }

                _licenceService.GenerateLicence(
                    _licenceService.GetHardwareId(),
                    expiryDate,
                    txtLicencePath.Text);

                MessageBox.Show("Licence生成成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"生成Licence失败: {ex.Message}");
            }
        }

        private void btnValidate_Click(object sender, EventArgs e)
        {
            try
            {
                var strValid = _licenceService.ValidateLicence(txtLicencePath.Text);
                MessageBox.Show(string.IsNullOrEmpty(strValid) ? "Licence验证通过" : strValid);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"验证Licence失败: {ex.Message}");
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (var dialog = new OpenFileDialog())
            {
                dialog.Filter = "Licence文件 (*.dat)|*.dat|所有文件 (*.*)|*.*";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    txtLicencePath.Text = dialog.FileName;
                }
            }
        }
    }
}